const db = require("../db/index");

// 获取用户的信息
exports.getUserinfo = (req, res) => {
  let sqlStr =
    "select id,username,user_pic,nickname,email from ev_users where id = ?";
  db.query(sqlStr, req.user.id, (err, results) => {
    if (err) return res.cc(err);
    if (results.length !== 1) {
      return res.cc("查询用户信息失败！");
    }
    res.send({
      status: "0",
      message: "获取用户信息成功！",
      data: results[0],
    });
  });
};

// 更新用户信息
exports.updateUserinfo = (req, res) => {
  let sqlStr = "update ev_users set ? where id = ? ";
  console.log(req.body);
  db.query(sqlStr, [req.body, req.body.id], (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) {
      return res.cc("修改用户信息失败！");
    }
    res.send({
      status: "0",
      message: "修改用户信息成功！",
      data: results[0],
    });
  });
};

// 更新用户头像
exports.updateUserPic = (req, res) => {
  let sqlStr = "update ev_users set user_pic = ? where id = ?";
  db.query(sqlStr, [req.body.avatar, req.user.id], (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) {
      return res.cc("修改头像失败！");
    }
    res.send({
      status: "0",
      message: "修改用户头像成功！",
      data: results,
    });
  });
};
